package com.wgts.wgts.mvp.view.activity.classification;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.LazyBaseFragment;
import com.wgts.wgts.control.recycleview.CustomLineaLayoutManager;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerClassificationFragmentComponent;
import com.wgts.wgts.dagger.module.ClassificationFragmentModule;
import com.wgts.wgts.mvp.presenter.IClassificationActivityPresenter;
import com.wgts.wgts.mvp.view.IClassificationActivityView;
import com.wgts.wgts.mvp.view.activity.search.SearchActivity;
import com.wgts.wgts.netmodle.resp.AllClassTypeEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 分类页面
 */
public class ClassificationFragment extends LazyBaseFragment implements IClassificationActivityView {


    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.linear_search)
    LinearLayout linearSearch;
    @BindView(R.id.recycle_classification)
    RecyclerView recycleClassification;
    @BindView(R.id.recycle_classification_detail)
    RecyclerView recycleClassificationDetail;
    Unbinder unbinder;
    @BindView(R.id.img_fragment_search)
    ImageView imgFragmentSearch;
    @BindView(R.id.relative_fragment)
    RelativeLayout relativeFragment;
    @BindView(R.id.linear_title)
    LinearLayout linearTitle;
    @BindView(R.id.img_net_work_error)
    ImageView imgNetWorkError;
    @BindView(R.id.linear_net_error)
    LinearLayout linearNetError;
    private CustomLineaLayoutManager linearLayoutManager;

    private ClassificationAdapter adapter;

    private ClassificationDetailAdapter classificationDetailAdapter;


    private List<AllClassTypeEntity> firstEntity;

    private List<AllClassTypeEntity.Entity> secondEntity;


    @Inject
    IClassificationActivityPresenter presenter;


    @Override
    public void init() {

        DaggerClassificationFragmentComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .classificationFragmentModule(new ClassificationFragmentModule(this))
                .build()
                .inject(this);
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.classification_activity, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        linearLayoutManager = new CustomLineaLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleClassification.setLayoutManager(linearLayoutManager);


        linearLayoutManager = new CustomLineaLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleClassificationDetail.setLayoutManager(linearLayoutManager);

        linearTitle.setVisibility(View.GONE);
        relativeFragment.setVisibility(View.VISIBLE);

    }


    @OnClick({R.id.linear_back, R.id.linear_search, R.id.img_fragment_search, R.id.img_net_work_error})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.linear_back:

                break;
            case R.id.linear_search:

                break;
            case R.id.img_fragment_search:
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
                break;
            case R.id.img_net_work_error:
                onRefresh();
                break;
        }
    }

    @Override
    public void initResult(List<AllClassTypeEntity> entity) {
        linearNetError.setVisibility(View.GONE);
        firstEntity = entity;
        secondEntity = new ArrayList<>();
        if (!entity.isEmpty()) {
            entity.get(0).setSelect(true);
            secondEntity.clear();
            secondEntity.addAll(entity.get(0).subTypes);
            //secondEntity = entity.get(0).subTypes;
            classificationDetailAdapter = new ClassificationDetailAdapter(secondEntity, getContext());

        }


        adapter = new ClassificationAdapter(firstEntity, getContext());
        recycleClassification.setAdapter(adapter);
        adapter.setOnItemClickView(new ClassificationAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {
                for (int i = 0; i < firstEntity.size(); i++) {
                    firstEntity.get(i).setSelect(false);
                }
                firstEntity.get(postion).setSelect(true);
                adapter.notifyDataSetChanged();
                secondEntity.clear();
                secondEntity.addAll(firstEntity.get(postion).subTypes);
                //secondEntity = firstEntity.get(postion).subTypes;
                classificationDetailAdapter.notifyDataSetChanged();
            }
        });


        recycleClassificationDetail.setAdapter(classificationDetailAdapter);
    }

    @Override
    protected void loadData() {
        presenter.initClassType();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onError(int status, String msg) {
        linearNetError.setVisibility(View.VISIBLE);
    }

    private void onRefresh(){
        presenter.initClassType();
    }
}
